package com.github.linmt.dbdoc4j.autoconfigure;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.linmt.dbdoc4j.metadata.query.base.MetaDataQueryOperations;
import com.github.linmt.dbdoc4j.metadata.query.mysql.MysqlMetaDataQuery;
import dbdoc4j.configuration.Dbdoc4jCommonConfiguration;
import dbdoc4j.plugins.DocumentationGroupType;
import dbdoc4j.plugins.Pay;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.plugin.core.PluginRegistry;
import org.springframework.plugin.core.config.EnablePluginRegistries;

import javax.annotation.Resource;

/**
 * User: Linmt
 * Date: 2022/12/1
 * Time: 21:18
 * Description: No Description
 */
@Configuration
@ConditionalOnProperty(value = "dbdoc4j.enable", havingValue = "true")
@EnableConfigurationProperties({Dbdoc4jProperties.class})
@Import(value = Dbdoc4jCommonConfiguration.class)
public class Dbdoc4jPropertiesAutoConfiguration {

    @Resource
    PluginRegistry<Pay, DocumentationGroupType> orderOperatorPluginRegistry;

    @Bean
    public MetaDataQueryOperations metaDataQueryOperations(Dbdoc4jProperties dbDocProperties) {
        orderOperatorPluginRegistry.getPlugins();
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(dbDocProperties.getUrl());
        dataSource.setDriverClassName(dbDocProperties.getDriverClassName());
        dataSource.setUsername(dbDocProperties.getUsername());
        dataSource.setPassword(dbDocProperties.getPassword());
        return new MysqlMetaDataQuery(dataSource);
    }
}
